use sea_orm::{ColumnTrait, DatabaseConnection, DbErr, EntityTrait, QueryFilter};
use sky_pojo::entities::user::{self, Model};

pub async fn get_by_openid(db: DatabaseConnection, openid: String) -> Result<Option<Model>, DbErr> {
    user::Entity::find()
        .filter(user::Column::Openid.eq(openid))
        .one(&db)
        .await
}

pub async fn sign_up(db: DatabaseConnection, model: user::ActiveModel) -> Result<Model, DbErr> {
    user::Entity::insert(model).exec_with_returning(&db).await
}

pub async fn get_by_id(db: DatabaseConnection, user_id: i64) -> Result<Option<Model>, DbErr> {
    user::Entity::find_by_id(user_id).one(&db).await
}
